<?php
namespace backend\controllers;

use backend\models\AuthRule;
use common\components\BaseController;
use Yii;
use \common\helps\tools;
use \common\helps\code;
use yii\db\Query;


/**
 * AuthRule controller
 */
class AuthRuleController extends BaseController
{

    public $modelClass = 'backend\models\AuthRule';
    public function init()
    {
        parent::init();
    }


    /**
     * 获取后台权限管理列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetAuthRuleList(){
        $request =$this->request;
        $model = new AuthRule();
        $params = $request->queryParams;
        $total_data = $model->getAuthRuleList();
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);

        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['节点名称','节点地址','唯一字段'];
            foreach($total_data as $key=>$v){
                $excel_data[$key]['name']=$v['name'];
                $excel_data[$key]['title']=$v['title'];
                $excel_data[$key]['unique_key']=$v['unique_key'];
            }
            tools::ToPHPEXECL($tableHeader,$excel_data,'后台权限管理列表');
        }

        return code::success($data);

    }

    /**
     * 删除节点
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDeleteRule(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new AuthRule();
            $params = $request->bodyParams;
            if(empty($params['id'])){
                return code::error('','参数错误');
            }
            $res = $model->deleteAuthRule(intval($params['id']));
            if($res){
                return code::success();
            }else{
                return code::error('error');
            }

        }
    }

    /**
     * 编辑节点
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionEditRule(){
            if($this->request->isPost) {
                $admin_model = new AuthRule();
                $params = $this->request->bodyParams;
                if(!empty($params['type']) && $params['type']==2){ //编辑
                    $admin_model->scenario='update';
                }else{
                    return code::error('','参数错误');
                }
                $admin_model->load($params,'');
                if(!$admin_model->validate()){
                    $error = array_values($admin_model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$admin_model->editRole($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }
    }

    /**
     * 新增节点
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionAddRule(){
        if($this->request->isPost) {
            $admin_model = new AuthRule();
            $params = $this->request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //新增
                $admin_model->scenario='pre_create';
            }else{
                return code::error('','参数错误');
            }
            $admin_model->load($params,'');
            if(!$admin_model->validate()){
                $error = array_values($admin_model->getErrors());
                return code::error('',$error[0][0]);
            }
            $data=$admin_model->editRole($params);
            if(!empty($data['error'])){
                return code::error('',$data['error']);
            }else{
                return code::success();
            }
        }
    }

    /**
     * 获取节点详情
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetAuthRuleInfo(){
        $request =$this->request;
        $params = $request->queryParams;
        $admin_model = new AuthRule();
        if(empty($params['id'])){  return code::error('','参数错误');}
        $data = (new Query())
            ->select('*')
            ->from('auth_rule')
            ->where(['id'=>intval(trim($params['id']))])
            ->one();
        if(!empty($data)){
            $parent_title= $admin_model->find()->select('title')->where(['id'=>$data['pid']])->asArray()->one();
            $data['parent_title'] = empty($parent_title)?'':$parent_title['title'];
            return code::success($data);
        }else{
            return code::error('','未查询到相关数据');
        }

    }

}
